package jp.co.cos.ittrick.channel.controller.client;

import jp.co.cos.ittrick.channel.model.CategoryModel;
import jp.co.cos.ittrick.channel.service.CategoryService;
import jp.co.cos.ittrick.channel.service.ChannelService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class UpdateCategoryController extends Controller {

    CategoryService categoryService;
    ChannelService channelService;

    public UpdateCategoryController() {
        categoryService = new CategoryService();
        channelService = new ChannelService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String category = asString("category");
        String name = asString("name");
        String index = asString("index");

        // Modelの設定
        CategoryModel model = categoryService.getCategoryByKey(Integer.valueOf(category));
        if(model != null) {
            model.setName(name);
            model.setIndex(Integer.valueOf(index));

            // Insert
            categoryService.update(model);
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.getWriter().println("0");
        response.flushBuffer();

        return null;
    }
}
